<!--
  ~ DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  ~
  ~ Copyright (c) 2008-2011, Salzburg Research. All rights reserved.
  ~
  ~ Redistribution and use in source and binary forms, with or without
  ~ modification, are permitted provided that the following conditions are met:
  ~ - Redistributions of source code must retain the above copyright notice,
  ~   this list of conditions and the following disclaimer.
  ~ - Redistributions in binary form must reproduce the above copyright notice,
  ~   this list of conditions and the following disclaimer in the documentation
  ~   and/or other materials provided with the distribution.
  ~ - Neither the name of the KiWi Project nor the names of its contributors
  ~   may be used to endorse or promote products derived from this software
  ~   without specific prior written permission.
  ~
  ~ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  ~ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  ~ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  ~ ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  ~ LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  ~ CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  ~ SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  ~ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  ~ CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  ~ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  ~ POSSIBILITY OF SUCH DAMAGE.
  ~
  ~ Contributor(s):
  ~  Thomas Kurz
  ~
  ~ File Info:
  ~   User: Thomas Kurz
  ~   Date: 2011/02/16
  ~   Time: 22:24
  ~
  ~ Project: KiWi2, KiWi2 module
  -->

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>LMF Importer Test</title>
    <script type="text/javascript">
        var url = "http://localhost:8080/KiWi2/import"
        var file_mime;
        function uploadFile() {
            var file = document.getElementById('file_content').files[0];
            file_mime = file.type;
            getAsText(file);
        }
        function upload(mime,data) {
            var xhr = new XMLHttpRequest;

            xhr.open("PUT", url, true);
            xhr.onreadystatechange = function() {
                if (xhr.readyState === 4) {
                    alert("(Status "+xhr.status+") "+xhr.responseText);
                }
            };

            var m = document.getElementById('mime').value;
            if(m != "") mime =  m;
            var contentType = mime;
            xhr.setRequestHeader("Content-Type", contentType);

            // finally send the request as binary data
            xhr.sendAsBinary(data);
        }
        function getAsText(readFile) {

          var reader = new FileReader();

          // Read file into memory as UTF-16
          reader.readAsBinaryString(readFile);

          // Handle progress, success, and errors
          reader.onload = loaded;
          reader.onerror = errorHandler;
        }

        function loaded(evt) {
          // Obtain the read file data
          var data = evt.target.result;
          upload(file_mime,data)
        }

        function errorHandler(evt) {
          if(evt.target.error.code == evt.target.error.NOT_READABLE_ERR) {
            // The file could not be read
            alert("Error while reading file");
          }
        }
    </script>
</head>
<body>
    <dl>
      <dt>Importer</dt>
      <dd>
          Type<input id="mime" type="text" value=""> <br>
          Datei:<input type="file" id="file_content"><div id="progress"></div><br>
          <input type="button" value="submit" onclick="uploadFile()">
      </dd>
   </dl>
</body>
</html>